package com.cloud.gateway.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.common.BaseReturn;
import com.cloud.common.CloudUserUtils;
import com.cloud.common.IdCardUtils;
import com.cloud.server.data.user.UserInfo;
import com.cloud.server.domain.user.User;
import com.cloud.server.domain.user.UserIntegral;
import com.cloud.server.domain.user.UserStatus;
import com.cloud.server.service.user.UserIntegralService;
import com.cloud.server.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import static com.cloud.common.Md5Utils.getSaltMD5;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyuan
 * @since 2021-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    UserIntegralService userIntegralService;


    @RequestMapping("/userInfo")
    public BaseReturn userInfo(){
        System.out.println("----------------");
        try {
            UserInfo userInfo = new UserInfo();
            Long userId = CloudUserUtils.getUserId();
            User user = userService.getById(userId);
            UserIntegral queryUserIntegral = new UserIntegral();
            queryUserIntegral.setUserId(userId);
            QueryWrapper<UserIntegral> userIntegralQueryWrapper = new QueryWrapper<>();
            userIntegralQueryWrapper.setEntity(queryUserIntegral);
            UserIntegral userIntegral = userIntegralService.getOne(userIntegralQueryWrapper);
            BeanUtils.copyProperties(user,userInfo);
            BeanUtils.copyProperties(userIntegral,userInfo);
            return BaseReturn.build("200","查询成功",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseReturn.build("413",e.getMessage());
        }
    }


    @RequestMapping("/register")
    public BaseReturn register(@RequestBody User user){
        System.out.println("---------"+user.getRealName());
        if(StringUtils.isBlank(user.getMobile())){
            //手机号不能为空
            return BaseReturn.build("500","手机号不能为空");
        }
        if(StringUtils.isBlank(user.getIdClass())){
            //班级不能为空
            return BaseReturn.build("500","班级不能为空");
        }
        if (StringUtils.isBlank(user.getPassword())){
            //密码不能为空
            return BaseReturn.build("500","密码不能为空");
        }
        if (StringUtils.isBlank(user.getRealName())){
            //真实姓名不能为空
            return BaseReturn.build("500","真实姓名不能为空");
        }
        if (StringUtils.isBlank(user.getIdCardNo())){
            //身份证不能为空
            return BaseReturn.build("500","身份证不能为空");
        }else {
            //解析身份证
            String idCardNo = user.getIdCardNo();

            if (idCardNo.length() == 18) {
                boolean b = IdCardUtils.validateIdCard18(idCardNo);
                if(b){
                    /**
                     * 构建身份证基本信息
                     * */
                    buildUserByIdCard(user);
                }else {
                    return BaseReturn.build("500","身份证不合法");
                }
            }else if (idCardNo.length() == 15){
                boolean b = IdCardUtils.validateIdCard15(idCardNo);
                if(b){
                    buildUserByIdCard(user);
                }else {
                    return BaseReturn.build("500","身份证不合法");
                }

            }else {
                return BaseReturn.build("500","身份证不合法");
            }

        }

        User queryUser = getUser(user);
        if(queryUser != null){
            return BaseReturn.build("500","该用户已注册");
        }else {
            boolean save;
            try {
                user.setPassword(getSaltMD5(user.getPassword()));
                user.setUserStatus(UserStatus.ACTIVE);
                save = userService.saveStudent(user);
                if(save){
                    return BaseReturn.build("200","注册成功");
                }else {
                    return BaseReturn.build("500","注册失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return BaseReturn.build("500","注册失败");
            }
        }
    }

    private void buildUserByIdCard(User user) {
        String idCardNo = user.getIdCardNo();
        String genderByIdCard = IdCardUtils.getGenderByIdCard(idCardNo);
        int ageByIdCard = IdCardUtils.getAgeByIdCard(idCardNo);
        user.setAge(ageByIdCard);
        if("M".equals(genderByIdCard)){
            user.setGender(0);
        }else if("F".equals(genderByIdCard)){
            user.setGender(1);
        }

    }


    private User getUser(User user) {
        User queryUser = new User();
        queryUser.setIdCardNo(user.getIdCardNo());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.setEntity(queryUser);
        return userService.getOne(userQueryWrapper);

    }

}

